let Bmob = require('bmob.js')
let Promise = require('es6-promise.min.js')

function wxPromise(fn) {
  return function (obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function (res) {
        resolve(res)
      }

      obj.fail = function (res) {
        reject(res)
      }

      fn(obj)
    })
  }
}

function showTip(sms, icon, fun, t) {
    if (!t) {
        t = 1000;
    }
    wx.showToast({
        title: sms,
        icon: icon,
        duration: t,
        success: fun
    })
}

function showModal(c,t,fun) {
    if(!t)
        t='提示'
    wx.showModal({
        title: t,
        content: c,
        showCancel:false,
        success: fun
    })
}

function getScrollViewHeight(headerHeight, footerHeight) {
  let _heightWindow;
  let _heightScrollView;

  wx.getSystemInfo({
    success: function (res) {
      _heightWindow = res.windowHeight;
    }
  });
  _heightScrollView = _heightWindow - headerHeight - footerHeight;
  return _heightScrollView;
}

/**
 * 获取微信小程序的用户登录凭证
 */
function getOpenId(success) {

  Bmob.initialize("8bbd2fa87eece23337234b59c300fb2e", "0422786c4a1502011b37da0d9f437770")

  wxPromise(wx.login)()
    .then(function(res){

      Bmob.User.requestOpenId(res.code, {
        success: function (result) {
          success && success(result.openid)
        }
      })

    })
}

module.exports.showTip = showTip;
module.exports.showModal = showModal;
module.exports.getScrollViewHeight = getScrollViewHeight;
module.exports.getOpenId = getOpenId;